//
//  CacheViewController.m
//  Atlantis
//
//  Created by 何恩营 on 14-12-17.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

// 收藏页面
#import "CollectViewController.h"
#import "UIColor+AddColor.h"
#import "FinderViewController.h"
#import "Music.h"
#import "DataBaseHandler.h"
#import "PlayViewController.h"
@interface CacheViewController ()<UITableViewDataSource, UITableViewDelegate, UIAlertViewDelegate>

@property(nonatomic, retain)UITableView *tableView;
@property (nonatomic, assign)CGFloat width;
@property (nonatomic, assign)CGFloat height;

@end

@implementation CacheViewController

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
   
    }
    return self;
}

// 视图将要出现
- (void)viewWillAppear:(BOOL)animated
{
    // 始终空出导航栏的高度
    self.navigationController.navigationBar.translucent = NO;
    
    [super viewWillAppear:animated];
    // 建立数据库
    DataBaseHandler *dataBase = [DataBaseHandler shareInstance];
    // 打开数据库
    [dataBase openDB];
    // 查询所有
    NSMutableArray *collectArr = [dataBase selectAll];
    // 判断数据库中有无数据
    if ([[dataBase selectAll] count] == 0) {
        // 警告窗
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你未收藏任何活动" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alertView show];
        [alertView release];
        
    } else {
        
        //self.collectArr = collectArr;
        // 初始化并赋值
        self.collectArr = [NSMutableArray arrayWithArray: collectArr];
    }
    
    // 指定代理
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    // 表视图刷新
    [self.tableView reloadData];
    
    
}

// 点击警告窗确定按钮实现事件
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    // 如果点击确认
    if (buttonIndex == 0) {
        // 通过tabBar下标跳转页面
        [self.tabBarController setSelectedIndex:0];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
//    self.collectArr = [NSMutableArray array];
//    NSLog(@"12345566778");

    // 导航栏标题
    self.navigationItem.title = @"我的收藏";
    
 
    // 设置屏幕宽
    self.width = self.view.frame.size.width;
    // 设置屏幕高
    self.height = self.view.frame.size.height;
    // 表视图
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height) style:UITableViewStylePlain];
    
    
    // 视图加载
    [self.view addSubview:self.tableView];
    // 内存管理
    [_tableView release];

}
// cell的行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //NSLog(@"%ld", self.collectArr.count);
    return self.collectArr.count;
}

// 创建cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 静态cell重用池
    static NSString *strr = @"0502";
    // 创建cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:strr];
    // 判断cell是否为空
    if (cell == nil) {
        // 若为空, 则创建
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:strr];
    }
    // 获取对象
    Music *music = [self.collectArr objectAtIndex:indexPath.row];
    //NSLog(@"%@--------", music.trackId);
    
    // 赋值
    cell.textLabel.text = music.title;
    //cell.textLabel.text = [[self.collectArr objectAtIndex:indexPath.row] title];

    
    return cell;
}

// 点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 从表视图移除
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    // 建立单例播放视图
    PlayViewController *details = [PlayViewController sharePlayVC];
    
    // 建立数据库
    DataBaseHandler *dataBaseHandler = [DataBaseHandler shareInstance];
    // 数据库查询
    details.musicArr = [dataBaseHandler selectAll];
    // 获取对象
    Music *news = [self.collectArr objectAtIndex:indexPath.row];

    
    // 判断是否播放同一音频
    if (![details.music.trackId isEqual:news.trackId]) {
       
        // 页面传值
        details.currentMusic = indexPath.row;
        details.music = news;
        // 播放
        [details play];
    }
    // 推出界面
    [self.navigationController pushViewController:details animated:YES];
    
    
}


// 是否可以编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}
// 是否可以移动
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

//删除方法

// 点击删除按钮实现方法
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 数据库建立
    DataBaseHandler *user = [DataBaseHandler shareInstance];
    // 数据库打开
    [user openDB];
    // 建立对象
    Music *news = [self.collectArr objectAtIndex:indexPath.row];
    // 删除
    [user deleteMusicWithId:news.trackId];
    NSLog(@"%@", news.trackId);
    // 移除
    [self.collectArr removeObjectAtIndex:indexPath.row];
    // 从tableView移除
    [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
    // 刷新界面
    [tableView reloadData];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
